<template>
  <div class="settings-container">
    <div class="page-header">
      <h2>账户设置</h2>
      <p>管理您的账户安全和偏好设置</p>
    </div>

    <el-card class="settings-card">
      <el-tabs v-model="activeTab">
        <el-tab-pane label="安全设置" name="security">
          <div class="settings-section">
            <h3>密码设置</h3>
            <el-form :model="passwordForm" :rules="passwordRules" ref="passwordFormRef" label-width="120px">
              <el-form-item label="当前密码" prop="currentPassword">
                <el-input v-model="passwordForm.currentPassword" type="password" show-password />
              </el-form-item>
              <el-form-item label="新密码" prop="newPassword">
                <el-input v-model="passwordForm.newPassword" type="password" show-password />
              </el-form-item>
              <el-form-item label="确认新密码" prop="confirmPassword">
                <el-input v-model="passwordForm.confirmPassword" type="password" show-password />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="changePassword" :loading="changingPassword">
                  修改密码
                </el-button>
              </el-form-item>
            </el-form>
          </div>
        </el-tab-pane>

        <el-tab-pane label="通知设置" name="notifications">
          <div class="settings-section">
            <h3>通知偏好</h3>
            <el-form label-width="120px">
              <el-form-item label="邮件通知">
                <el-switch v-model="notificationSettings.email" />
              </el-form-item>
              <el-form-item label="短信通知">
                <el-switch v-model="notificationSettings.sms" />
              </el-form-item>
              <el-form-item label="系统通知">
                <el-switch v-model="notificationSettings.system" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="saveNotificationSettings">
                  保存设置
                </el-button>
              </el-form-item>
            </el-form>
          </div>
        </el-tab-pane>

        <el-tab-pane label="隐私设置" name="privacy">
          <div class="settings-section">
            <h3>隐私选项</h3>
            <el-form label-width="120px">
              <el-form-item label="公开个人信息">
                <el-switch v-model="privacySettings.publicProfile" />
              </el-form-item>
              <el-form-item label="显示调用统计">
                <el-switch v-model="privacySettings.showStats" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="savePrivacySettings">
                  保存设置
                </el-button>
              </el-form-item>
            </el-form>
          </div>
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'

const activeTab = ref('security')

// 密码表单
const passwordFormRef = ref()
const changingPassword = ref(false)

const passwordForm = reactive({
  currentPassword: '',
  newPassword: '',
  confirmPassword: ''
})

const passwordRules = {
  currentPassword: [
    { required: true, message: '请输入当前密码', trigger: 'blur' }
  ],
  newPassword: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 8, message: '密码长度不能少于8位', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认新密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== passwordForm.newPassword) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
}

// 通知设置
const notificationSettings = reactive({
  email: true,
  sms: false,
  system: true
})

// 隐私设置
const privacySettings = reactive({
  publicProfile: false,
  showStats: true
})

// 修改密码
const changePassword = async () => {
  if (!passwordFormRef.value) return
  
  try {
    await passwordFormRef.value.validate()
    changingPassword.value = true
    
    // 模拟API调用
    await new Promise(resolve => setTimeout(resolve, 1000))
    
    ElMessage.success('密码修改成功')
    passwordForm.currentPassword = ''
    passwordForm.newPassword = ''
    passwordForm.confirmPassword = ''
    passwordFormRef.value.resetFields()
  } catch (error) {
    console.error('修改密码失败:', error)
  } finally {
    changingPassword.value = false
  }
}

// 保存通知设置
const saveNotificationSettings = () => {
  ElMessage.success('通知设置保存成功')
}

// 保存隐私设置
const savePrivacySettings = () => {
  ElMessage.success('隐私设置保存成功')
}
</script>

<style scoped lang="scss">
.settings-container {
  padding: 20px;
  background-color: #f5f5f5;
  min-height: calc(100vh - 60px);
}

.page-header {
  margin-bottom: 24px;
  text-align: center;
  
  h2 {
    font-size: 28px;
    color: #303133;
    margin-bottom: 8px;
  }
  
  p {
    color: #909399;
    font-size: 14px;
  }
}

.settings-card {
  max-width: 800px;
  margin: 0 auto;
}

.settings-section {
  h3 {
    margin-bottom: 20px;
    color: #303133;
    font-size: 18px;
  }
}
</style>
